package mediacodec;

import android.annotation.TargetApi;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

import java.io.IOException;

import customview.ike.com.customviewproject.R;

/**
 * Created by Administrator on 2017/12/22.
 */

public class MediaCodecActivity extends AppCompatActivity implements SurfaceHolder.Callback, Camera.PreviewCallback {
    private String Tag="MediaCodecActivity";
    private SurfaceView mSurfaceView;
    private Camera mCamera;
    private VideoEncoder encoder;
    private boolean isStarted;
    private byte[] buffer;
    private CameraRecorder mp4Recorder;
    public static final int IMAGE_HEIGHT = 720;
    public static final int IMAGE_WIDTH = 1280;
    private byte[] mImageCallbackBuffer = new byte[IMAGE_WIDTH
            *IMAGE_HEIGHT * 3 / 2];

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_codec);
        mSurfaceView= (SurfaceView) findViewById(R.id.surfaceView);
        SurfaceHolder holder = mSurfaceView.getHolder();
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        holder.addCallback(this);
        encoder=new VideoEncoder(this);
        
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera = Camera.open(0);
            Camera.Parameters parameters = mCamera.getParameters();
            parameters.setPreviewFormat(ImageFormat.NV21);
            parameters.setPreviewSize(IMAGE_WIDTH, IMAGE_HEIGHT);
//            avcEncoder.setVideoOptions(parameters.getPreviewSize().width,
//                    parameters.getPreviewSize().height, 320000, 25);
            parameters.setPreviewFormat(ImageFormat.NV21);
            mCamera.setParameters(parameters);
            mCamera.addCallbackBuffer(mImageCallbackBuffer);
            mCamera.setPreviewCallback(this);
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        if (isStarted){
            encoder.fireVideo(data);
//            mp4Recorder.feedData(data,System.currentTimeMillis());
            mCamera.addCallbackBuffer(mImageCallbackBuffer);
        }
       
    }
  @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
  public  void encode(View view){
        if (!isStarted){
            isStarted=true;
            encoder.startEncoder();
            //startCamera();
           
        }else {
            //停止录制
            isStarted=false;
            encoder.stopEncoder();
            Log.e(Tag,"视屏地址:"+encoder.savePath);
        }
      
  }
  public void startCamera(){
      long timeCount=0;
//                    if(mRecorder==null){
//                        mRecorder=new CameraRecorder();
//                        mAudioEncoder=new AudioEncoder();
//                    }
      if(mp4Recorder==null){
          mp4Recorder=new CameraRecorder();
      }
      long time=System.currentTimeMillis();
      String savePath=encoder.getPath("video/",time+".mp4");
      mp4Recorder.setSavePath(encoder.getPath("video/",time+""),"mp4");
//                    mAudioEncoder.setSavePath(getVideoPath(time+".aac"));
      try {
//                        mRecorder.prepare(360,640);
//                        mRecorder.start();
          mp4Recorder.prepare(384,640);
          mp4Recorder.start();
          
      } catch (IOException | InterruptedException e) {
          e.printStackTrace();
      }
  }
    
}
